package com.sita.tboard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sita.bike.R;
import com.sita.bike.model.EaseUser2;
import com.sita.bike.persistence.TrendMember;
import com.sita.bike.rest.RestClient;
import com.sita.bike.rest.model.Account;
import com.sita.bike.rest.model.Rank;
import com.sita.bike.rest.model.request.GetAccountRequest;
import com.sita.bike.rest.model.response.GetAccountResponse;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.AccountUtils;
import com.sita.bike.utils.FormatUtils;
import com.sita.bike.utils.PersistUtils;
import com.sita.friend.FriendHelper;
import com.sita.tboard.ui.tools.L;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by zhangdepeng on 2016/2/1.
 */
public class RankingAdapter extends BaseAdapter {

    public List<Rank> mRankList;
    public Context mContext;

    public RankingAdapter(Context context, List<Rank> rank) {
        this.mRankList = rank;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mRankList.size();
    }

    @Override
    public Object getItem(int position) {
        return mRankList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.rank_listview_item, null);
            viewHolder = new ViewHolder();
            viewHolder.img_medal = (ImageView) convertView.findViewById(R.id.medal);
            viewHolder.img_user_head = (ImageView) convertView.findViewById(R.id.user_head_img);
            viewHolder.tv_nick = (TextView) convertView.findViewById(R.id.tv_nick_rank_name);
            viewHolder.tv_priods = (TextView) convertView.findViewById(R.id.priods);
            viewHolder.tv_num = (TextView) convertView.findViewById(R.id.num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int i = position;
        switch (position) {
            case 0:
                viewHolder.img_medal.setImageResource(R.drawable.gold_medal);
                viewHolder.img_medal.setVisibility(View.VISIBLE);
                viewHolder.tv_num.setVisibility(View.INVISIBLE);
                break;
            case 1:
                viewHolder.img_medal.setImageResource(R.drawable.silver_medal);
                viewHolder.img_medal.setVisibility(View.VISIBLE);
                viewHolder.tv_num.setVisibility(View.INVISIBLE);
                break;
            case 2:
                viewHolder.img_medal.setImageResource(R.drawable.bronze_medal);
                viewHolder.img_medal.setVisibility(View.VISIBLE);
                viewHolder.tv_num.setVisibility(View.INVISIBLE);
                break;
            default:
                viewHolder.tv_num.setText("" + (position + 1));
                viewHolder.tv_num.setVisibility(View.VISIBLE);
                viewHolder.img_medal.setVisibility(View.INVISIBLE);
                break;

        }

        Rank rank = mRankList.get(position);

        String avatar = null;
        String nickname = null;
        if (rank.accountId.equals(AccountUtils.getAccountID())){
            avatar = AccountUtils.getAvatar();
            nickname = AccountUtils.getNickName();
            viewHolder.tv_nick.setText(nickname);
            Picasso.with(GlobalContext.getGlobalContext()).load(avatar).placeholder(R.drawable.default_head)
                    .fit().centerInside().into(viewHolder.img_user_head);
        } else {
            EaseUser2 user = FriendHelper.getInstance().getContactUser(rank.accountId);
            if (user != null) {
                avatar = user.getAvatar();
                nickname = user.getNick();
                viewHolder.tv_nick.setText(nickname);
                Picasso.with(GlobalContext.getGlobalContext()).load(avatar).placeholder(R.drawable.default_head)
                        .fit().centerInside().into(viewHolder.img_user_head);
            } else {
                TrendMember trendMember = PersistUtils.getTrendMember(rank.accountId);
                if (trendMember != null) {
                    avatar = trendMember.getAvatarUrl();
                    nickname = trendMember.getNickname();
                    viewHolder.tv_nick.setText(nickname);
                    Picasso.with(GlobalContext.getGlobalContext()).load(avatar).placeholder(R.drawable.default_head)
                            .fit().centerInside().into(viewHolder.img_user_head);
                } else {
                    GetAccountRequest req = new GetAccountRequest();
                    req.accountId = rank.accountId;
                    RestClient.getRestNormalService().accountGet(req, new Callback<GetAccountResponse>() {
                        @Override
                        public void success(GetAccountResponse resp, Response response) {
                            Account account = resp.account;

                            viewHolder.tv_nick.setText(account.mNickName);
                            Picasso.with(GlobalContext.getGlobalContext()).load(account.mAvatar).placeholder(R.drawable.default_head)
                                    .fit().centerInside().into(viewHolder.img_user_head);

                            PersistUtils.saveTrendMember(account);
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            L.e(error);
                        }
                    });
                }
            }
        }





        if (rank.mMileage > 0) {
            viewHolder.tv_priods.setText("" + FormatUtils.doubleDistance1((mRankList.get(position).mMileage) / 1000) + "Km");
        } else if (rank.mPeriods == 0) {
            viewHolder.tv_priods.setText("" + 0 + "Km");
        }


        return convertView;
    }

    class ViewHolder {
        ImageView img_medal;
        ImageView img_user_head;
        TextView tv_nick;
        TextView tv_priods;
        TextView tv_num;
    }
}
